/*******************************************************************************
 * @brief: systask
 * @Author: aple
 * @Date: 2022-12-04 10:37:03
 * @LastEditors: aple
 * @LastEditTime: 2022-12-07 19:27:41
 * @Verision: 1.0.0.0
 ******************************************************************************/
#include "bsp_api.h"
#include "sys_task.h"
#include "pincfg.h"
#include "mytype.h"
#include "sys_init.h"
#include "os_obj.h"
#include "framework.h"
#include "bsp_adc.h"
#include "filtern.h"
#include "lib_surport.h"
#include "crc_ext.h"
#include "swap.h"

/*****************************************************************************
 * @brief   order
 *****************************************************************************/
obj_init_order4(system_info);
obj_init_order4(task_init);
obj_app_order(light_manage);
obj_app_order(check_handle);
obj_app_order(adc_resault_handle);

/*****************************************************************************
 * @brief   var
 *****************************************************************************/
sys_run_st_type Gst;

/*****************************************************************************
 * @brief   fun
 *****************************************************************************/
void task_run_arg_init(void);
void err_handle(void);

/*****************************************************************************
 * @brief   system task.
 *****************************************************************************/
void task_system(const void *argument)
{
	dbg_puts("start\n");

	thread_create();

	sys_app_init();

	app_tick_resume();

	while (1)
	{
		sys_app_run_once();

		sc_pulse_creat(app_tick_get());
		Gst.hal.utick = app_tick_get();

		// rtc
		if (0 == Gst.hal.rtc_ok)
		{
			if (sc_pulse.pulse_1s)
			{
				sig_time_type stim;
				// TimeRun(&Gst.hal.sim_rtc);
				rtc_time_run_sec(&Gst.hal.sim_rtc, 1);
				Gst.hal.rtc = time_to_sig(&Gst.hal.sim_rtc);
			}
		}
		else
		{
			time_sig_to_time(Gst.hal.rtc, &Gst.hal.sim_rtc);
		}

		sub2_add8(10, 102);

		osSignalWait(0, ms_ticks(20));
	}
}

/*****************************************************************************
 * @brief   system init.
 *****************************************************************************/
void task_init(void)
{
	void *buff;
}

/*******************************************************************************
 * @brief: adc resault handle
 * @param {*}
 * @return {*}
 ******************************************************************************/
void adc_resault_handle(void)
{
	UU32 sum;
	
	if(!adc_man.st_hal_end)
		return;
	adc_man.st_hal_end = 0;
	
	loop(ADC_CH_NUM)
	{
		sum = sum_u32(adc_man.list[index], array_len(adc_man.list[index]));
		adc_man.val[index] = sum / 800;
	}
}

/*******************************************************************************
 * @brief: speed filter
 * @param {*}
 * @return {*}
 ******************************************************************************/
void light_manage(void)
{
	static word_tt led = {1};
	
	sequence_linkage_manage(&Gst.sout, Gst.hal.utick, 2000);
	
	led0.out(Gst.sout.out1);
	led1.out(Gst.sout.out2);

	if (sc_pulse.pulse_250ms)
	{
		led.uval = bit16_roll_shift_left(led.uval, 4, 1);
		// led0.out(led.bits.b0);
		led2.out(led.bits.b1);
		// led2.out(led.bits.b2);
		// led2.tog();
	}
}

/*******************************************************************************
 * @brief: check handle
 * @param {*}
 * @return {*}
 ******************************************************************************/
void check_handle(void)
{
	uint32_t lrc;

#define LRC_ADDR01 0x20000000
#define LRC_ADDR02 0x20000004
#define LRC_ADDR0_RES 0x20000008
#define LRC_ADDR0_NRES 0x2000000C

	// 输出状态
	lrc = 0;
	lrc += inpw(LRC_ADDR01);
	lrc += inpw(LRC_ADDR02);
	outpw(LRC_ADDR0_RES, lrc);
	outpw(LRC_ADDR0_NRES, ~lrc);
	/*
		sys_par.mcu_mark_cnt.ival = lib_mark_get();
		sys_par.mcu_last_cnt.ival = lib_chk_get();
		sys_par.mcu_min.ival = lib_chk_min_get();
		sys_par.mcu_max.ival = lib_chk_max_get();*/
}

/*******************************************************************************
 * @brief: check
 * @param argc 参数数量
 * @param argv 参数指针
 * @return 错误代码
 ******************************************************************************/
int lib_check(int argc, char *argv[])
{
	int cnt = 1;
	if(argc > 1)
		cnt = atoi(argv[1]);
	while(cnt--)
	{
		lib_chk_test();
		check_handle();
	}
	dbg_print("res type : %d\n", lib_chk_err_type());
	dbg_print("fault last : %d\n", lib_chk_get());
	return 0;
}
DBG_FUN_EXPORT(lib_check);

/*****************************************************************************
 * @brief   system info.
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void system_info(void)
{
	/*
		dbg_puts(SYMBOL_LINE_EQ PTCR);

		dbg_puts(asc_pic_light);

		dbg_puts(SYMBOL_LINE_EQ PTCR);
		dbg_print("HCLK = %d MHZ, PCLK1 = %d MHZ, PCLK2 = %d MHZ\n",
				  HAL_RCC_GetHCLKFreq() / 1000000,
				  HAL_RCC_GetPCLK1Freq() / 1000000,
				  HAL_RCC_GetPCLK2Freq() / 1000000);
		dbg_puts(SYMBOL_LINE_EQ PTCR);*/
}

/*****************************************************************************
 * @brief   fatfs time
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
uint32_t get_fattime(void)
{
	sig_time_type tim;

	tim.val = Gst.hal.rtc.val;

	tim.time.year += 20;

	return tim.val;
}
